package cn.zh.stan.wxapi;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import cn.zh.stan.MyApplication;
import cn.zh.stan.R;
import cn.zh.stan.WX.Constants;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler, View.OnClickListener {
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
    private IWXAPI api;

    Button close;

    TextView txt;
   MyApplication mapp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
		mapp= (MyApplication) getApplication();
		close=findViewById(R.id.close);
		txt=findViewById(R.id.txt);
    	api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
		close.setOnClickListener(this);
		if (mapp.isIspay()){
			txt.setText("支付成功");
		}else {
			txt.setText("支付失败");
			mapp.setIspay(true);
		 }
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@SuppressLint("LongLogTag")
	@Override
	public void onResp(BaseResp resp) {
		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			if (0==resp.errCode){
				txt.setText("支付成功");
			}else {
				txt.setText("支付失败");
			}
//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setTitle(R.string.app_tip);
//			builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
//			builder.show();
		}
	}

	@Override
	public void onClick(View view) {
		int id=view.getId();
		switch (id){
			case R.id.close:
				finish();
				break;
		}
	}
}